<?php
namespace app\equi\controller;

class Useseal extends Base{


	public function __construct(){
		parent::__construct();
		$this->use = model("useseal");
		//$this->audit = model("usesealaudit");
		$this->log = model("usesealLog");
		$this->option = model("Option");

		$this->flog();
	}


	public function flog(){
		// $s ='';
		// $s .= print_r($_SERVER,true);
		// $s .= print_r($_GET,true);
		// $s .= print_r($_POST,true);
		// $s .= print_r($_FILES,true);


		// // if(isset($_FILES['start_img'])){
		// // 	move_uploaded_file($_FILES['start_img']['tmp_name'],'./s_'.date("Y-m-d_His").'.jpg');
		// // }
		// // if(isset($_FILES['end_img'])){
		// // 	move_uploaded_file($_FILES['end_img']['tmp_name'],'./e_'.date("Y-m-d_His").'.jpg');
		// // }

		// file_put_contents('aaaaaaaaaaa.txt', $s ,FILE_APPEND );

	}


	// 用章申请列表
	// http://192.168.200.99/equi/useseal/getlist
	// page=1&pagesize=5&type_id=1&apply_user_id=3&equipment_id=10
	public function getlist(){
		$this->_mustPost();

		$page = input("page");
		$pagesize = input("pagesize");
		$type_id = input("type_id"); // 1用章 2维护 
		$apply_user_id = input("apply_user_id"); 
		$equipment_id = input("equipment_id");



		$res = $this->use->eqGetList($page,$pagesize,$type_id,$apply_user_id,$equipment_id);

		if(is_array($res)){
			$this->res["data"] = $res;
		}else{
			$this->res["code"] = 999;
			$this->res["msg"] = $res;
		}


		die(myJSON($this->res));

		
	}

	

	//获取用章详细信息
	//http://192.168.200.99/equi/useseal/getdetails
	//id=12
	public function getdetails(){
		$this->_mustPost();
		$id = input("id/d");

		$res = $this->use->eqGetDetails($id);

		if(is_array($res)){
			$this->res["data"] = $res;
		}else{
			$this->res["code"] = 999;
			$this->res["msg"] = $res;
		}


		die(myJSON($this->res));

	}



	// 盖章前检查此次盖章是否符合条件
	// http://127.0.2.1/equi/useseal/usecheck
	// id=6
	// equipment_id=1
	public function usecheck(){
		$this->_mustPost();
		$id = input("id/d");
		$equipment_id = input("equipment_id/d");
		$res = $this->use->find($id);;

		if(!$res){
			$this->res["code"] = 999;
			$this->res["msg"] = '没有数据';
			die(myJSON($this->res));
		}

		if( $res['type_id'] != 1){
			$this->res["code"] = 999;
			$this->res["msg"] = '这不是一个用章申请';
			die(myJSON($this->res));
		}


		if( $res['equipment_id'] != $equipment_id ){
			$this->res["code"] = 999;
			$this->res["msg"] = '当前设备不对';
			die(myJSON($this->res));
		}

		if(  !in_array( $res['status'] , [2,3] )){
			$this->res["code"] = 1201;
			$this->res["msg"] = '状态：'.$res['status_text'].'，无法盖章。';
			die(myJSON($this->res));
		}

		if(  ($res['max_times'] - $res['use_times']) <= 0 ){
			$this->res["code"] = 1202;
			$this->res["msg"] = '使用次数已用完';
			$res->where('id',$res["id"])->setField('status', 4);
			die(myJSON($this->res));
		}
		//过期
		if( (strtotime($res['exp_time'])+24*3600 -time()) <=0 ){
			$this->res["code"] = 1203;
			$this->res["msg"] = '已经过期';
			$res->where('id',$res["id"])->setField('status', 5);
			die(myJSON($this->res));
			//UPDATE `bc_useseal` SET `status` = '5' WHERE status <4 and datediff(now(),exp_time) < 5;
		}
	
		die(myJSON($this->res));
	}




	// 记录盖章次数
	// https://v2.seal.film521.com/equi/useseal/uselog
	// useseal_id=6&apply_user_id=3&start_time=1231313123123&end_time=21312312312312&start_img=start_img&end_img=end_img
	// {"code":0,"msg":"ok","data":{"max_times":1,"use_times":5}} 
	public function uselog(){
		//$this->_mustPost();
		//
		$res = $this->log->eqSaveData();
		if(is_array($res)){
			$this->res["data"] = $res;
		}else{
			$this->res["code"] = 999;
			$this->res["msg"] = $res;
		}
		die(myJSON($this->res));
		
	}

	//5.2 进入维护模式前。
	//http://192.168.200.99/equi/useseal/whcheck
	//id=7&equipment_id=1

	public function whcheck(){
		$this->_mustPost();
		$id = input("id/d");
		$equipment_id = input("equipment_id/d");
		$res = $this->use->find($id);;

		if(!$res){
			$this->res["code"] = 999;
			$this->res["msg"] = '没有数据';
			die(myJSON($this->res));
		}


		if( $res['type_id'] != 2){
			$this->res["code"] = 999;
			$this->res["msg"] = '这不是一个维护申请';
			die(myJSON($this->res));
		}


		if( $res['equipment_id'] != $equipment_id ){
			$this->res["code"] = 999;
			$this->res["msg"] = '当前设备不对';
			die(myJSON($this->res));
		}

		if(  !in_array( $res['status'] , [2,3] )){
			$this->res["code"] = 1201;
			$this->res["msg"] = '状态：'.$res['status'].$res['status_text'].'，无法维护 。';
			die(myJSON($this->res));
		}

		//过期
		if( (strtotime($res['exp_time'])+24*3600 -time()) <=0 ){
			$this->res["code"] = 1203;
			$this->res["msg"] = '已经过期';
			$res->where('id',$res["id"])->setField('status', 5);
			die(myJSON($this->res));
			//UPDATE `bc_useseal` SET `status` = '5' WHERE status <4 and datediff(now(),exp_time) < 5;
		}



		die(myJSON($this->res));

	}


	//5.3 读取印章信息
	//http://192.168.200.99/equi/useseal/getsealinfo
	// equipment_id=1
	public  function getsealinfo(){
		$this->_mustPost();
		$equipment_id = input("equipment_id/d");

		//获取设备 印章信息
		// $seal = model('seal')->where('equipment_id' ,  $equipment_id )->order(' position asc ')->column(' id , seal_name , seal_type , equipment_id , position ' , 'position' ) ; //印章列表
		$seal = model('seal')->where('equipment_id' ,  $equipment_id )->order(' position asc ')->select(); //印章列表
		$opts11 = opts(11);
		foreach($seal as $k => $v){
			$v['seal_type_text'] = isset( $opts11[$v['seal_type']] ) ? $opts11[$v['seal_type']]  : '未知-'.$v['seal_type'];
			$seal[$k] = $v;
		};

		if(!$seal){
			$this->res["code"] = 1209;
			$this->res["msg"] = '读取设备'.$equipment_id .'印章信息失败';
			die(myJSON($this->res));
		}

		$this->res["data"] =  $seal ;

		die(myJSON($this->res));

	}

	//维护完成
	public function whfinish(){
		$this->_mustPost();
		$id = input("id/d");
		$rs = $this->use->where('id' , $id )->setField('status', 4 );

		die(myJSON($this->res));
	}



}